var AdminScriptManager = {};


/*----------------------------------------------------------------------*/
/* INIT */
AdminScriptManager.Init = function () {


};

/*----------------------------------------------------------------------*/
/* INITHANDLERS */
AdminScriptManager.InitHandlers = function () {

	/*-----------------------------------------------------------------------------------------------------------*/
	/* CATEGORY EXPAND/COLLAPSE CLICK */
	$("DIV.categoryHeader").children("SPAN.collapseExpand, SPAN.categoryTitle").click(function () {
		var $this = $(this);

		$this.parent().siblings("UL.scripts").slideToggle("fast");
		$this.parent().children("SPAN.collapseExpand").toggleClass("blueArrowDownIcon blueArrowRightIcon");
	});

	/*-----------------------------------------------------------------------------------------------------------*/
	/* DELETE CLICK */
	$("BUTTON.btnDelete").click(function () {
		var script = $(this).closest(".script");
		var scriptID = script.attr("scriptid");
		var scriptTitle = script.attr("scripttitle");

		AdminScriptManager.DeleteScript(scriptID, scriptTitle);
	});

};


/* END INITHANDLER */



/*----------------------------------------------------------------------*/
/* DELETE SCRIPT */
AdminScriptManager.DeleteScript = function (scriptID, scriptTitle) {

	var html = [
        "<div id='dlgDelete'>",
            "<div id='dlgDelete_Content'>",
				"Are you sure you want to DELETE script '" + scriptTitle + "' ? ",
				"<br>Once deleted, it cannot be undone.",
			"</div>",
        "</div>"
    ];




	var onSuccess = function (response) {
		if (response.Success) {
			var script = $("LI.script[scriptid=" + scriptID + "]");
			var scripts = script.closest("UL.scripts");

			script.remove();

			if (scripts.children().length == 0) {
				scripts.closest("LI.category").remove();
			}

		}
		else {
			alert("Failed to delete script. Please try again. If the Script continues to fail then please report it with this information:\n" +
				"Error: " + data.ErrorMessage);
		}
	};



	var onError = function (jqXHR, textStatus, errorThrown) {
		alert("Failed to delete script. Please try again. If the Script continues to fail then please report it with this information:\n" +
				"Error: " + errorThrown);
	};


	var onComplete = function () {
		$("#dlgDelete").remove();
	};


	$(html.join(""))
        .dialog({
        	modal: true,
        	width: 800,
        	title: "Delete Script",
        	buttons: {
        		"Yes": function () {
        			$("#dlgDelete_Content").showWhiteSquareWaiting(
						{
							callback: function () {
								$.ajax({
									url: "AJAX/AdminScriptEditor.asp?method=DeleteScript",
									type: "POST",
									success: onSuccess,
									error: onError,
									complete: onComplete,
									data: {
										ScriptID: scriptID
									}
								});
							}
						}
					)

        		},
        		"No": function () {
        			$(this).dialog("close");
        		}
        	},
        	close: function (e, ui) {
        		$("#dlgDelete").remove();
        	}
        });



};




/*=======================================================================*/
$(document).ready(function(){
	AdminScriptManager.Init();
	AdminScriptManager.InitHandlers();
});